import json
import os
from rouge import *
import jieba

def read_json(full_p):
    d = json.load(open(full_p, encoding='utf-8'))
    return d

def get_data(dir):
    datas = []
    for file in os.listdir(dir):
        if file.endswith('json'):
            full_p = os.path.join(dir, file)
            d = json.load(open(full_p, encoding='utf-8'))
            d['id'] = int(file.split('.')[0])
            # print(d)
            g = lambda x:x.replace('\r','').replace('\n','')
            d['question'] = g(d['question'] )
            d['candidate_answer'] = [g(u) for u in d['candidate_answer']]
            datas.append(d)
    return datas

def get_best_ans(candidate_answer,ans):
    stats = []
    for pred_ans in candidate_answer:
        ans_ = ' '.join(jieba.lcut(ans))
        pred = ' '.join(jieba.lcut(pred_ans))
        rouge = Rouge()
        score = 0
        try:
            rouge_score = rouge.get_scores([pred], [ans_])
            r1 = rouge_score[0]['rouge-1']['r']
            r2 = rouge_score[0]['rouge-2']['r']
            rl = rouge_score[0]['rouge-l']['r']
            #print(r1,r2,rl)
            score = 0.2* r1 + 0.3 * r2 + 0.5* rl
            #stats.append((r1,r2,rl))

        except :
            print('X',pred)
        stats.append((pred_ans,score))
    return stats